package toy.keli.picmap.service;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;

import androidx.annotation.Nullable;

import toy.keli.picmap.model.types.PhotoItem;
import toy.keli.picmap.service.Connection.Bind;
/**
 * server https://www.jianshu.com/p/738105190328
 */
public class FindPicService extends Service {
    public static final String TAG = "FindPicService";
    private Connection.Bind mBind;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        if (mBind != null) {
            return mBind;
        }
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBind = new Bind();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                initData();
            }
        }).start();
//        Intent stopIntent = new Intent(this, FindPicService.class);
//        stopService(stopIntent);
        return super.onStartCommand(intent, flags, startId);
    }

    private void initData() {
        // 获取SD卡的文件，有的比较耗时，一般我们不在Main-Thread线程中去做
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(uri,new String[]{
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATE_ADDED,
                MediaStore.Images.Media._ID},null,null,null,null);
        while (cursor.moveToNext()) {
            PhotoItem photoItem = new PhotoItem();
            photoItem.setPath(cursor.getString(0));
            photoItem.setName(cursor.getString(1));
            photoItem.setCreateTime(cursor.getLong(2));
//            mDatas.add(photoItem);
        }
        cursor.close();
    }
}
